package com.happyscan138.makefun.Fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.PopupWindow;

import com.happyscan138.makefun.BaseAdapter.my_collection;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.adapter.myCollectionAdapter;
import com.happyscan138.makefun.base.LoadListView;
import com.happyscan138.makefun.questionDetailed;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static android.content.Context.MODE_PRIVATE;
import static com.happyscan138.makefun.Urls.Urls.base;
import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.happyscan138.makefun.base.share.access_key;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/5/24.
 */

public class MyCollectionQuestuion extends Fragment implements LoadListView.IloadListener, View.OnClickListener {
    private List<my_collection> Listdata;
    private LoadListView listView_myDiscuss_question;
    private SharedPreferences preferences;
    private String like_type_id;
    private PopupWindow pop;
    private Intent intent;
    private my_collection data;
    private int LIKE_ID;
    private Handler handler;
    private ArrayList<Integer> ID;
    private myCollectionAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mydiscuss_question, null);
        preferences = getContext().getSharedPreferences("UserInfo", MODE_PRIVATE);
        access_key = preferences.getString("UserId", "");
        like_type_id = preferences.getString("LikeTypeID", "");
        ID = new ArrayList<Integer>();
        initView(view);
        getAdapterView();

        return view;
    }

    public void initView(View view) {
        listView_myDiscuss_question = (LoadListView) view.findViewById(R.id.mydiscuss_question_listview);
        listView_myDiscuss_question.setInterface(this);
    }

    public void getAdapterView() {

        Listdata = new ArrayList<>();
        client = new AsyncHttpClient();
        params = new RequestParams();
        params.put("access_key", access_key);
        params.put("like_type_id", like_type_id);
        params.put("page_count", 10);
        params.put("like_id_index", 0);
        log.e("params", params.toString());
        RequestHandle handle = client.post(Urls.MyCollection, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr = response.toString();
                try {
                    JSONObject jsonObject = new JSONObject(jsonStr);
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject ObjData = jsonArray.getJSONObject(i);
                        LIKE_ID = ObjData.getInt("like_id");
                        ID.add(LIKE_ID);
                        data = new my_collection();
                        data.head = base + ObjData.getString("img_path");
                        data.name = ObjData.getString("nick_name");
                        data.time = ObjData.getString("create_date");
                        data.text = ObjData.getString("content");
                        data.resource = base + ObjData.getString("resource");
                        data.problem_id = ObjData.getString("problem_id");
                        data.like_id = ObjData.getString("like_id");
                        log.e("ObjData", ObjData.toString());
                        Listdata.add(data);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                adapter = new myCollectionAdapter(getContext(), Listdata, access_key);
                listView_myDiscuss_question.setAdapter(adapter);
                listView_myDiscuss_question.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        log.e("itemclick", "ssssssssssssssssss");
                        intent = new Intent();
                        data = Listdata.get(position);
                        intent.putExtra("problem_id", data.problem_id);
                        intent.setClass(getContext(), questionDetailed.class);
                        startActivity(intent);
                    }
                });
            }
        });

    }

    @Override
    public void onLoad() {
        handler=new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                params=new RequestParams();
                params.put("access_key",access_key);
                params.put("like_type_id",like_type_id);
                params.put("page_count",10);
                params.put("like_id_index",ID.get(ID.size()-1));
                log.e("params",params.toString());
                RequestHandle handle=client.post(Urls.MyCollection,params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr=response.toString();
                        try {
                            JSONObject jsonObject=new JSONObject(jsonStr);
                            JSONArray jsonArray=jsonObject.getJSONArray("data");
                            for(int i=0;i<jsonArray.length();i++){
                                JSONObject ObjData=jsonArray.getJSONObject(i);
                                data=new my_collection();
                                data.head=base+ObjData.getString("img_path");
                                data.name=ObjData.getString("nick_name");
                                data.time=ObjData.getString("create_date");
                                data.text=ObjData.getString("content");
                                data.resource=base+ObjData.getString("resource");
                                data.problem_id=ObjData.getString("problem_id");
                                data.like_id=ObjData.getString("like_id");
                                log.e("ObjData",ObjData.toString());
                                Listdata.add(data);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
                listView_myDiscuss_question.loadComplete();
            }
        },2000);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_collection_back:
                getActivity().finish();
                break;

        }
    }
}